<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>采购管理</el-breadcrumb-item>
      <el-breadcrumb-item>采购明细列表</el-breadcrumb-item>
    </el-breadcrumb>
     <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getPurchaseDetails">
          <el-button slot="append" icon="el-icon-search" @click="getPurchaseDetails"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">
            添加采购明细
          </el-button>
        </el-col>
      </el-row>
    <!-- 用户列表区 -->
      <!-- // 采购明细号，采购清单号（外码），商品编号（外码），采购数量，商品单价，商品总价，备注。
//  purchasedetailid purchasedetailmasterid purchasedetailgoodsid purchasedetailnum purchasedetailgoodsprice purchasedetailgoodstotle purchasedetailothers -->
      <el-table :data="purchasemasterlist" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="采购明细号" prop="purchasedetailid"></el-table-column>
        <el-table-column label="采购清单号" prop="purchasedetailmasterid"></el-table-column>
        <el-table-column label="商品编号" prop="purchasedetailgoodsid"></el-table-column>
        <el-table-column label="采购数量" prop="purchasedetailnum"></el-table-column>
        <el-table-column label="商品单价" prop="purchasedetailgoodsprice"></el-table-column>
        <el-table-column label="商品总价" prop="purchasedetailgoodstotle"></el-table-column>
        <el-table-column label="备注" prop="purchasedetailothers"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-tooltip effect="dark" content="修改采购主表" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.purchasedetailid)"></el-button>
            </el-tooltip>
            <!-- 删除按钮 -->
            <el-tooltip effect="dark" content="删除采购主表" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeWorkerByID(scope.row.purchasedetailid)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 2, 5, 10, 20, 30, 40, 50]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
    </el-card>
    <!-- 添加用户的对话框 -->
    <el-dialog
      title="采购主表信息"
      :visible.sync="addDialogVisible"
      width="50%" @close="addDialogClosed">
      <!-- 内容主体区 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="80px">
        <!-- // 采购明细号，采购清单号（外码），商品编号（外码），采购数量，商品单价，商品总价，备注。
//  purchasedetailid purchasedetailmasterid purchasedetailgoodsid purchasedetailnum purchasedetailgoodsprice purchasedetailgoodstotle purchasedetailothers -->
        <el-form-item label="采购明细号" prop="purchasedetailid">
          <el-input v-model="addForm.purchasedetailid"></el-input>
        </el-form-item>
        <el-form-item label="采购清单号" prop="purchasedetailmasterid">
          <el-input v-model="addForm.purchasedetailmasterid"></el-input>
        </el-form-item>
        <el-form-item label="商品编号" prop="purchasedetailgoodsid">
          <el-input v-model="addForm.purchasedetailgoodsid"></el-input>
        </el-form-item>
        <el-form-item label="采购数量" prop="purchasedetailnum">
          <el-input v-model="addForm.purchasedetailnum"></el-input>
        </el-form-item>
        <el-form-item label="商品单价" prop="purchasedetailgoodsprice">
          <el-input v-model="addForm.purchasedetailgoodsprice"></el-input>
        </el-form-item>
        <el-form-item label="商品总价" prop="purchasedetailgoodstotle">
          <el-input v-model="addForm.purchasedetailgoodstotle"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="purchasedetailothers">
          <el-input v-model="addForm.purchasedetailothers"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addPurchaseDetail">确 定</el-button>
      </span>
      <!-- 修改用户的对话框 -->
    </el-dialog>
    <el-dialog
      title="修改商品"
      :visible.sync="editDialogVisible"
      width="50%" @close="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="80px">
             <!-- // 采购明细号，采购清单号（外码），商品编号（外码），采购数量，商品单价，商品总价，备注。
//  purchasedetailid purchasedetailmasterid purchasedetailgoodsid purchasedetailnum purchasedetailgoodsprice purchasedetailgoodstotle purchasedetailothers -->
        <el-form-item label="采购明细号" prop="purchasedetailid">
          <el-input v-model="editForm.purchasedetailid" disabled></el-input>
        </el-form-item>
        <el-form-item label="采购清单号" prop="purchasedetailmasterid">
          <el-input v-model="editForm.purchasedetailmasterid"></el-input>
        </el-form-item>
        <el-form-item label="商品编号" prop="purchasedetailgoodsid">
          <el-input v-model="editForm.purchasedetailgoodsid"></el-input>
        </el-form-item>
        <el-form-item label="采购数量" prop="purchasedetailnum">
          <el-input v-model="editForm.purchasedetailnum"></el-input>
        </el-form-item>
        <el-form-item label="商品单价" prop="purchasedetailgoodsprice">
          <el-input v-model="editForm.purchasedetailgoodsprice"></el-input>
        </el-form-item>
        <el-form-item label="商品总价" prop="purchasedetailgoodstotle">
          <el-input v-model="editForm.purchasedetailgoodstotle"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="purchasedetailothers">
          <el-input v-model="editForm.purchasedetailothers"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editPurchaseDetail">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除用户的对话框 -->
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 获取员工列表的参数
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前的页面大小
        pagesize: 10
      },
      purchasemasterlist: [],
      total: 0,
      // 控制添加用户对话框的显示与隐藏
      addDialogVisible: false,
      // 这是添加的表单对象
      // purchasedetailid purchasedetailmasterid purchasedetailgoodsid purchasedetailnum purchasedetailgoodsprice purchasedetailgoodstotle purchasedetailothers
      addForm: {
        purchasedetailid: '',
        purchasedetailmasterid: '',
        purchasedetailgoodsid: '',
        purchasedetailnum: '',
        purchasedetailgoodsprice: '',
        purchasedetailgoodstotle: '',
        purchasedetailothers: ''
      },
      // 这是编辑用户的表单对象
      editForm: {},
      // 控制修改用户对话框的显示与隐藏
      editDialogVisible: false,
      // 添加表单规则的对象
      addFormRules: {
        // 采购明细号，采购清单号（外码），商品编号（外码），采购数量，商品单价，商品总价，备注。
        purchasedetailid: [
          { required: true, message: '请输入采购明细号', trigger: 'blur' },
          { min: 3, max: 10, message: '采购明细号的长度在3~10个字符之间', trigger: 'blur' }
        ],
        purchasedetailmasterid: [
          { required: true, message: '请输入采购清单号', trigger: 'blur' },
          { min: 3, max: 10, message: '采购清单的长度在3~10个字符之间', trigger: 'blur' }
        ],
        purchasedetailgoodsid: [
          { required: true, message: '请输入商品编号', trigger: 'blur' },
          { min: 3, max: 10, message: '商品编号的长度在3~10个字符之间', trigger: 'blur' }
        ],
        purchasedetailnum: [
          { required: true, message: '请输入采购数量', trigger: 'blur' },
          { min: 3, max: 10, message: '采购数量的长度在3~10个字符之间', trigger: 'blur' }
        ],
        purchasedetailgoodsprice: [
          { required: true, message: '请输入商品单价', trigger: 'blur' },
          { min: 3, max: 10, message: '商品单价的长度在3~10个字符之间', trigger: 'blur' }
        ],
        purchasedetailgoodstotle: [
          { required: true, message: '请输入商品总价', trigger: 'blur' },
          { min: 1, max: 20, message: '备注的长度在1-20个字符之间', trigger: 'blur' }
        ],
        purchasedetailothers: [
          { required: true, message: '请输入备注', trigger: 'blur' },
          { min: 0, max: 200, message: '商品编号的长度在0~200个字符之间', trigger: 'blur' }
        ]
      },
      editFormRules: {
        purchasedetailid: [
          { required: true, message: '请输入采购明细号', trigger: 'blur' },
          { min: 3, max: 10, message: '采购明细号的长度在3~10个字符之间', trigger: 'blur' }
        ],
        purchasedetailmasterid: [
          { required: true, message: '请输入采购清单号', trigger: 'blur' },
          { min: 3, max: 10, message: '采购清单的长度在3~10个字符之间', trigger: 'blur' }
        ],
        purchasedetailgoodsid: [
          { required: true, message: '请输入商品编号', trigger: 'blur' },
          { min: 3, max: 10, message: '商品编号的长度在3~10个字符之间', trigger: 'blur' }
        ],
        purchasedetailnum: [
          { required: true, message: '请输入采购数量', trigger: 'blur' },
          { min: 3, max: 10, message: '采购数量的长度在3~10个字符之间', trigger: 'blur' }
        ],
        purchasedetailgoodsprice: [
          { required: true, message: '请输入商品单价', trigger: 'blur' },
          { min: 3, max: 10, message: '商品单价的长度在3~10个字符之间', trigger: 'blur' }
        ],
        purchasedetailgoodstotle: [
          { required: true, message: '请输入商品总价', trigger: 'blur' },
          { min: 1, max: 20, message: '备注的长度在1-20个字符之间', trigger: 'blur' }
        ],
        purchasedetailothers: [
          { required: true, message: '请输入备注', trigger: 'blur' },
          { min: 0, max: 200, message: '商品编号的长度在0~200个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getPurchaseDetails()
  },
  methods: {
    async getPurchaseDetails () {
      const { data: res } = await this.$http.get('/purchase_detail', { params: this.queryInfo })
      if (res.code !== 20000) return this.$message.error('获取采购主表失败')
      this.purchasemasterlist = res.data.value
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getPurchaseDetails()
    },
    // 监听 pagenum 改变的事件
    handleCurrentChange (newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getPurchaseDetails()
    },
    // 监听表单关闭
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    // 添加新用户
    addPurchaseDetail () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) {
          return valid
        }
        // 添加网络校验请求
        const { data: res } = await this.$http.post('purchase_detail', this.addForm)
        if (res.code === 30002) return this.$message.error('该采购明细已经存在')
        if (res.code !== 20000) return this.$message.error('添加采购明细失败')
        this.$message.success('添加采购明细成功')
        this.addDialogVisible = false
        // 从新获取用户列表
        this.getPurchaseDetails()
      })
    },
    // 展示编辑用户的对话框
    async showEditDialog (id) {
      this.queryInfo.query = id
      const { data: res } = await this.$http.get('purchase_detail', { params: this.queryInfo })
      if (res.code !== 20000) {
        return this.$message.error('查询采购主表信息失败')
      }
      this.editForm = res.data.value[0]
      this.editDialogVisible = true
    },
    // 监听修改用户对话框的关闭事件
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 修改用户信息并提交
    editPurchaseDetail () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return console.log(valid)
        // purchasedetailid purchasedetailmasterid purchasedetailgoodsid purchasedetailnum purchasedetailgoodsprice purchasedetailgoodstotle purchasedetailothers
        const { data: res } = await this.$http.put('purchase_detail', {
          purchasedetailid: this.editForm.purchasedetailid,
          purchasedetailmasterid: this.editForm.purchasedetailmasterid,
          purchasedetailgoodsid: this.editForm.purchasedetailgoodsid,
          purchasedetailnum: this.editForm.purchasedetailnum,
          purchasedetailgoodsprice: this.editForm.purchasedetailgoodsprice,
          purchasedetailgoodstotle: this.editForm.purchasedetailgoodstotle,
          purchasedetailothers: this.editForm.purchasedetailothers
        })
        if (res.code !== 20000) return this.$message.error('更新采购主表失败!')
        // 关闭对话框
        this.editDialogVisible = false
        // 刷新数据列表
        this.getPurchaseDetails()
        // 爱上的健康
        this.$message.success('更新用户信息成功')
      })
    },
    // 根据 id 值来删除用户
    async removeWorkerByID (id) {
      // 弹框询问用户是否删除
      const confirmResult = await this.$confirm('此操作将永久删除该采购明细, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 如果点确定返回一个字符串 confirm, 如果取消返回字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除')
      }
      console.log(id)
      const { data: res } = await this.$http.delete('purchase_detail/' + id)
      if (res.code !== 20000) {
        return this.$message.error('删除采购主表失败！')
      }
      this.$message.success('删除采购主表成功！')
      this.getPurchaseDetails()
    }
  }
}
</script>

<style lang="less" scoped>

</style>
